package com.wtc.houserent.view;

import com.wtc.houserent.model.House;
import com.wtc.houserent.service.HouseService;
import com.wtc.houserent.utils.Utility;

/**
 * 1、显示界面
 * 2、接收用户的输入
 * 3、调用HouseService完成对房屋信息的各种操作
 */
public class HouseView {
    private boolean loop = true;
    private char key = ' ';
    private HouseService houseService = new HouseService(2);

    //显示主菜单
    public void mainMenu() {
        do {
            System.out.println("————————房屋出租系统————————");
            System.out.println("\t\t\t\t\t\t1 新 增 房 源");
            System.out.println("\t\t\t\t\t\t2 查 找 房 屋");
            System.out.println("\t\t\t\t\t\t3 删 除 房 屋");
            System.out.println("\t\t\t\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t\t\t\t6 退            出");
            System.out.print("请输入你的选项(1-6)：");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    addHouse();
                    break;
                case '2':
                   selectHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    exit();
                    break;
            }
        } while (loop);
    }
    //显示房屋列表方法
    public void listHouse() {
        System.out.println("—————————房屋列表—————————");
        System.out.println("编号\t房主\t电话\t地址\t月租\t状态（未出租/已出租）");
        House[] house = houseService.house();
        for (int i = 0; i < house.length; i++) {
            if (house[i] == null) {
                continue;
            }
            System.out.println(house[i]);
        }
        System.out.println("房屋列表显示完毕！");
    }
    //添加房屋方法
    public void addHouse() {
        System.out.println("—————————添加房屋—————————");
        System.out.println("请输入你要添加的房屋信息");
        System.out.print("姓名：");
        String name = Utility.readString(10);
        System.out.print("电话：");
        String phone = Utility.readString(10);
        System.out.print("地址：");
        String address = Utility.readString(20);
        System.out.print("月租：");
        int rent = Utility.readInt();
        System.out.print("状态：");
        String state = Utility.readString(10);
        House newHouse = new House(0, name, phone, address, rent, state);
        if (houseService.add(newHouse)) {
            System.out.println("房屋信息添加成功！");
        } else {
            System.out.println("房屋信息添加失败！");
        }
    }
    //删除房屋信息方法
    public void delHouse() {
        System.out.println("—————————添加房屋—————————");
        System.out.print("请输入你要删除的房屋信息id(谨慎删除，-1退出删除)：");
        int delId = Utility.readInt();
        if (delId == -1) {
            System.out.println("退出删除功能！");
            return;
        }
        char choice = Utility.readConfirmSelection();
        if (choice == 'Y') {
            if (houseService.del(delId)) {
                System.out.println("删除成功！");
            } else {
                System.out.println("删除失败！你输入的ID有误！");
            }
        } else {
            System.out.println("取消删除！");
        }
    }
    //退出方法
    public void exit() {
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            loop = false;
        }
    }
    public void selectHouse() {
        System.out.println("—————————查询房屋—————————");
        System.out.print("请输入你要查询的房屋信息id：");
        int selectId = Utility.readInt();
        if (! houseService.select(selectId)) {
            System.out.println("查询失败！房屋ID不存在！");
        }
    }
    public void updateHouse() {
        System.out.println("—————————查询房屋—————————");
        System.out.print("请输入你要修改的房屋信息id：");
        int updateId = Utility.readInt();
        House update = houseService.update(updateId);
        if (update != null) {
            System.out.print("姓名(" + update.getName() + ")：");
            String updateName = Utility.readString(10, update.getName());
            update.setName(updateName);
            System.out.print("电话(" + update.getPhone() + ")：");
            String updatePhone = Utility.readString(11, update.getPhone());
            update.setPhone(updatePhone);
            System.out.print("地址(" + update.getAddress() + ")：");
            String updateAddress = Utility.readString(20, update.getAddress());
            update.setAddress(updateAddress);
            System.out.print("租金(" + update.getRent() + ")：");
            int updateRent = Utility.readInt(update.getRent());
            update.setRent(updateRent);
            System.out.print("状态(" + update.getState() + ")：");
            String updateState = Utility.readString(10, update.getState());
            update.setState(updateState);
            System.out.println("修改完成！");
        } else {
            System.out.println("修改失败！房屋id不存在！");
        }
    }
}
